/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GenerarSolicitud.java
 *
 * Created on 13/01/2012, 11:26:11 PM
 */

package sst.Vistas;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;


/**
 *
 * @author alberto
 */
public class GenerarSolicitud extends javax.swing.JDialog {
    Connection con;
    /** Creates new form GenerarSolicitud */
    public GenerarSolicitud(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String URL = "jdbc:mysql://localhost/sst";
                con = (Connection) DriverManager.getConnection(URL,"root","alberto.1990");
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Problemas con la conexion", "Falla en DB", JOptionPane.ERROR_MESSAGE);
            } catch (ClassNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "Problemas con el driver de conexion", "Falla en driver ", JOptionPane.ERROR_MESSAGE);
            }
            initComponents();
            setLocationRelativeTo(null);
            java.sql.Statement consultaCliente = con.createStatement();
            java.sql.Statement consultaSolicitud = con.createStatement();
            java.sql.Statement consultaTecnico = con.createStatement();
            java.sql.Statement consultaEstado = con.createStatement();
            java.sql.ResultSet res = consultaCliente.executeQuery("SELECT nombreCliente FROM cliente");
            java.sql.ResultSet res1 = consultaSolicitud.executeQuery("SELECT nombreSolicitud FROM tipoSolicitud");
            java.sql.ResultSet res2 = consultaTecnico.executeQuery("SELECT nombreCompleto FROM tecnico");
            java.sql.ResultSet res3 = consultaEstado.executeQuery("SELECT estadoSolicitud FROM estadoSolicitud");

            while (res.next()) {
                ListaClientes.addItem(res.getString("nombreCliente"));
            }

            while (res1.next()){
            ListaSolicitud.addItem(res1.getString("nombreSolicitud"));
            }

             while (res2.next()) {
                ListaTecnico.addItem(res2.getString("nombreCompleto"));
            }

             while (res3.next()) {
                ListaEstado.addItem(res3.getString("estadoSolicitud"));
            }

           // con.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Error en la consulta de Clientes", "Falla en DB", JOptionPane.ERROR_MESSAGE);
        }
        



    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        ListaClientes = new javax.swing.JComboBox();
        botonNuevoCli = new javax.swing.JButton();
        botonConsultaCli = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        ListaSolicitud = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        areaDescripcion = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        ListaTecnico = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        areaObservacion = new javax.swing.JTextArea();
        botonGenerarSol = new javax.swing.JButton();
        botonVaciar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        ListaEstado = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        jLabel1.setText("Generar Solicitud para Soporte Tecnico");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 695, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        jLabel2.setText("Cliente:");

        botonNuevoCli.setText("Nuevo");
        botonNuevoCli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNuevoCliActionPerformed(evt);
            }
        });

        botonConsultaCli.setText("Consultar");
        botonConsultaCli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonConsultaCliActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        jLabel3.setText("Tipo de Solicitud:");

        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        jLabel4.setText("Descripcion:");

        areaDescripcion.setColumns(20);
        areaDescripcion.setRows(5);
        jScrollPane1.setViewportView(areaDescripcion);

        jLabel5.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        jLabel5.setText("Tecnico a Cargo:");

        jLabel6.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        jLabel6.setText("Observacion:");

        areaObservacion.setColumns(20);
        areaObservacion.setRows(5);
        jScrollPane2.setViewportView(areaObservacion);

        botonGenerarSol.setText("Generar");
        botonGenerarSol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGenerarSolActionPerformed(evt);
            }
        });

        botonVaciar.setText("Vaciar");
        botonVaciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonVaciarActionPerformed(evt);
            }
        });

        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        jLabel7.setText("Estado Actual:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(174, 174, 174)
                .addComponent(jLabel1)
                .addContainerGap(213, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(39, 39, 39)
                        .addComponent(ListaEstado, 0, 330, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1)
                            .addComponent(ListaClientes, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ListaSolicitud, 0, 327, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                            .addComponent(ListaTecnico, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(botonGenerarSol, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(botonVaciar, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(27, 27, 27)
                .addComponent(botonNuevoCli, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonConsultaCli)
                .addGap(21, 21, 21))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(ListaClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonConsultaCli)
                    .addComponent(botonNuevoCli))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(ListaSolicitud, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(ListaTecnico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel7))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(ListaEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonGenerarSol)
                    .addComponent(botonVaciar)
                    .addComponent(botonCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed

    private void botonVaciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonVaciarActionPerformed
        ListaClientes.setSelectedIndex(0);
        ListaSolicitud.setSelectedIndex(0);
        ListaTecnico.setSelectedIndex(0);
        ListaEstado.setSelectedIndex(0);
        areaDescripcion.setText("");
        areaObservacion.setText("");
    }//GEN-LAST:event_botonVaciarActionPerformed

    private void botonGenerarSolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGenerarSolActionPerformed
        
            try {
                String rif = "";
                int cedulaTecnico=0;
                String nombreTec= ListaTecnico.getSelectedItem().toString();
                String nombreCli = ListaClientes.getSelectedItem().toString();
                java.sql.Statement consultaRif = con.createStatement();
                java.sql.ResultSet res = consultaRif.executeQuery("SELECT rif FROM cliente WHERE nombreCliente = '" + nombreCli + "'");
                java.sql.Statement consultaTec = con.createStatement();
                java.sql.ResultSet res2 = consultaTec.executeQuery("SELECT cedula FROM tecnico WHERE nombreCompleto = '"+nombreTec+"'");
                while (res.next()) {
            rif = res.getString("rif");
            while(res2.next())
            cedulaTecnico = res2.getInt("cedula");


            int solicitud = ListaSolicitud.getSelectedIndex();
            String descripcion = areaDescripcion.getText();
            int tecnico = ListaTecnico.getSelectedIndex();
            String observacion = areaObservacion.getText();
            int estado = ListaEstado.getSelectedIndex();
            java.sql.PreparedStatement consulta = con.prepareStatement("INSERT INTO solicitud VALUES (?,?,?,?,?,?,?)");
            consulta.setInt(1,0);
            consulta.setString(2, descripcion);
            consulta.setString(3, observacion);
            consulta.setString(4, rif);
            consulta.setInt(5, estado);
            consulta.setInt(6, solicitud);
            consulta.setInt(7,cedulaTecnico);
            consulta.executeUpdate();
            JOptionPane.showMessageDialog(this, "Solicitud Generada", "Guardado Exitoso", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Error en la consulta de Clientes", "Falla en DB", JOptionPane.ERROR_MESSAGE);
            } 
        
           


    }//GEN-LAST:event_botonGenerarSolActionPerformed

    private void botonNuevoCliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNuevoCliActionPerformed
       
        try {
            Principal b = new Principal();
            RegistroCliente a = new RegistroCliente(b, true);
            this.dispose();
            a.setVisible(true);
            
        } catch (SQLException ex) {
            Logger.getLogger(GenerarSolicitud.class.getName()).log(Level.SEVERE, null, ex);
        }

        
    }//GEN-LAST:event_botonNuevoCliActionPerformed

    private void botonConsultaCliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonConsultaCliActionPerformed
       BuscaCliente a = new BuscaCliente();
       a.setVisible(true);
    }//GEN-LAST:event_botonConsultaCliActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GenerarSolicitud dialog = new GenerarSolicitud(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ListaClientes;
    private javax.swing.JComboBox ListaEstado;
    private javax.swing.JComboBox ListaSolicitud;
    private javax.swing.JComboBox ListaTecnico;
    private javax.swing.JTextArea areaDescripcion;
    private javax.swing.JTextArea areaObservacion;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JButton botonConsultaCli;
    private javax.swing.JButton botonGenerarSol;
    private javax.swing.JButton botonNuevoCli;
    private javax.swing.JButton botonVaciar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

}
